package com.grm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文章高级查询
 *
 * @author gaorimao
 * @date 2022/03/25
 */
@RestController
@RequestMapping("/elk")
public class ArticleAdvanceController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private ElasticsearchOperations operations;

    /**
     * 分页查询
     *
     * @param pageNum  页码,从0开始
     * @param pageSize 分页大小
     * @return 查询结果
     */
    @GetMapping("/queryPage")
    public String queryPage(@RequestParam int pageNum, @RequestParam int pageSize) {
//        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
//        query.setPageable(PageRequest.of(pageNum, pageSize));
//
//        // 方法1
//        SearchHits<Article> search = restTemplate.search(query, Article.class);
//        // 方法2
//        // SearchHits<Article> search = operations.search(query, Article.class);
//        List<Article> articles = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
//        return articles.toString();

        return "";
    }

    /**
     * 滚动查询
     *
     * @param scrollId 滚动id
     * @param pageSize 分页大小
     * @return 查询结果
     */
    @GetMapping(value = "/scrollQuery")
    public String scroll(String scrollId, Integer pageSize) {
//        if (pageSize == null || pageSize <= 0) {
//            return "please input query page num";
//        }
//        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
//        query.setPageable(PageRequest.of(0, pageSize));
//        SearchHits<Article> searchHits;
//        if (StringUtils.isEmpty(scrollId) || scrollId.equals("0")) {
//            // 开启一个滚动查询,设置该scroll上下文存在60s
//            // 同一个scroll上下文,只需要设置一次query(查询条件)
//            searchHits = restTemplate.searchScrollStart(60000, query, Article.class, IndexCoordinates.of("article"));
//            if (searchHits instanceof SearchHitsImpl) {
//                scrollId = ((SearchHitsImpl) searchHits).getScrollId();
//            }
//        } else {
//            // 继续滚动
//            searchHits = restTemplate.searchScrollContinue(scrollId, 60000, Article.class, IndexCoordinates.of("article"));
//        }
//        List<Article> articles = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
//        if (articles.size() == 0) {
//            // 结束滚动
//            restTemplate.searchScrollClear(Collections.singletonList(scrollId));
//            scrollId = null;
//        }
//        if (Objects.isNull(scrollId)) {
//            Map<String, String> result = new HashMap<>(2);
//            result.put("articles", articles.toString());
//            result.put("message", "已到末尾");
//            return result.toString();
//        } else {
//            Map<String, String> result = new HashMap<>();
//            result.put("count", String.valueOf(searchHits.getTotalHits()));
//            result.put("pageSize", String.valueOf(articles.size()));
//            result.put("articles", articles.toString());
//            result.put("scrollId", scrollId);
//            return result.toString();
//        }
        return "";
    }
}

